package com.zygh.policeasset.component;

import com.zygh.policeasset.mapper.AssetsNoMapper;
import com.zygh.policeasset.pojo.AssetsNo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Component
public class AssetsCode {
    @Autowired
    AssetsNoMapper assetsNoMapper;

    public String getZNJC(String type, String witbigNum, String witsmallNum, String stopTime, String buildNum, Integer sectionId) {
        if (StringUtils.isEmpty(this.assetsNoMapper.findByType(type, buildNum, sectionId))) {
            Integer integer = this.assetsNoMapper.selectCounter(type, stopTime, null);
            if (!StringUtils.isEmpty(integer)) {
                this.assetsNoMapper.add(type, buildNum, sectionId, Integer.valueOf(integer.intValue() + 1), stopTime, null);
            } else {
                this.assetsNoMapper.add(type, buildNum, sectionId, 1, stopTime, null);
            }
        } else {
            this.assetsNoMapper.update(type, buildNum, sectionId);
        }
        AssetsNo no = this.assetsNoMapper.findByType(type, buildNum, sectionId);
        if (type == null || type.equals("") || witbigNum == null || witbigNum.equals("") || witsmallNum == null || witsmallNum.equals("") || stopTime == null || stopTime.equals("")) {
            return "";
        }
        StringBuffer code = new StringBuffer();
        code.append(type);
        code.append(witbigNum);
        code.append(witsmallNum);
        code.append(stopTime.replace("-", ""));
        for (int i = 1; i < 6 - String.valueOf(no.getCounter()).length(); i++) {
            code.append("0");
        }
        code.append(no.getCounter());
        return code.toString();
    }

    public String getZD(String type, String stopTime, String departmentNum, String buildNum) {
        if (StringUtils.isEmpty(this.assetsNoMapper.findByType(type, buildNum, null))) {
            Integer integer = this.assetsNoMapper.selectCounter(type, stopTime, departmentNum);
            if (!StringUtils.isEmpty(integer)) {
                this.assetsNoMapper.add(type, buildNum, null, Integer.valueOf(integer.intValue() + 1), stopTime, departmentNum);
            } else {
                this.assetsNoMapper.add(type, buildNum, null, 1, stopTime, departmentNum);
            }
        } else {
            this.assetsNoMapper.update(type, buildNum, null);
        }
        AssetsNo no = this.assetsNoMapper.findByType(type, buildNum, null);
        if (type == null || type.equals("") || departmentNum == null || departmentNum.equals("") || stopTime == null || stopTime.equals("")) {
            return "";
        }
        StringBuffer code = new StringBuffer();
        code.append(type);
        code.append(departmentNum);
        code.append("01");
        code.append(stopTime.replace("-", ""));
        for (int i = 1; i < 6 - String.valueOf(no.getCounter()).length(); i++) {
            code.append("0");
        }
        code.append(no.getCounter());
        return code.toString();
    }
}